// DisplayLayout.cpp
#include "DisplayLayout.h"

const bool DisplayLayout::digitPatterns[12][7][3] = {
  { {1,1,1}, {1,0,1}, {1,0,1}, {1,0,1}, {1,0,1}, {1,0,1}, {1,1,1} },
  { {0,1,0}, {1,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {1,1,1} },
  { {1,1,1}, {0,0,1}, {0,0,1}, {1,1,1}, {1,0,0}, {1,0,0}, {1,1,1} },
  { {1,1,1}, {0,0,1}, {0,0,1}, {0,1,1}, {0,0,1}, {0,0,1}, {1,1,1} },
  { {1,0,1}, {1,0,1}, {1,0,1}, {1,1,1}, {0,0,1}, {0,0,1}, {0,0,1} },
  { {1,1,1}, {1,0,0}, {1,0,0}, {1,1,1}, {0,0,1}, {0,0,1}, {1,1,1} },
  { {1,1,1}, {1,0,0}, {1,0,0}, {1,1,1}, {1,0,1}, {1,0,1}, {1,1,1} },
  { {1,1,1}, {0,0,1}, {0,0,1}, {0,0,1}, {0,0,1}, {0,0,1}, {0,0,1} },
  { {1,1,1}, {1,0,1}, {1,0,1}, {1,1,1}, {1,0,1}, {1,0,1}, {1,1,1} },
  { {1,1,1}, {1,0,1}, {1,0,1}, {1,1,1}, {0,0,1}, {0,0,1}, {1,1,1} },
  // 字母A (数字10)
  { {0,1,0}, {1,0,1}, {1,0,1}, {1,1,1}, {1,0,1}, {1,0,1}, {1,0,1} },
  
  // 字母P (数字11)
  { {1,1,1}, {1,0,1}, {1,0,1}, {1,1,1}, {1,0,0}, {1,0,0}, {1,0,0} }
};

const bool DisplayLayout::smallDigitPatterns[10][5][3] = {
  { {1,1,1}, {1,0,1}, {1,0,1}, {1,0,1}, {1,1,1} },
  { {0,1,0}, {1,1,0}, {0,1,0}, {0,1,0}, {1,1,1} },
  { {1,1,1}, {0,0,1}, {1,1,1}, {1,0,0}, {1,1,1} },
  { {1,1,1}, {0,0,1}, {0,1,1}, {0,0,1}, {1,1,1} },
  { {1,0,1}, {1,0,1}, {1,1,1}, {0,0,1}, {0,0,1} },
  { {1,1,1}, {1,0,0}, {1,1,1}, {0,0,1}, {1,1,1} },
  { {1,1,1}, {1,0,0}, {1,1,1}, {1,0,1}, {1,1,1} },
  { {1,1,1}, {0,0,1}, {0,0,1}, {0,0,1}, {0,0,1} },
  { {1,1,1}, {1,0,1}, {1,1,1}, {1,0,1}, {1,1,1} },
  { {1,1,1}, {1,0,1}, {1,1,1}, {0,0,1}, {1,1,1} }
};

DisplayLayout::DisplayLayout(uint16_t width, uint16_t height) 
    : width(width), height(height) {
    // 恢复默认布局配置
    timeX = (width - getTimeDisplayWidth()) / 2;
    timeY = 0;
    dateX = (width - getDateDisplayWidth()) / 2;
    dateY = 1;
}

void DisplayLayout::setTimePosition(uint8_t x, uint8_t y) {
    if(x < width && y < height) {
        timeX = x;
        timeY = y;
    }
}

void DisplayLayout::setDatePosition(uint8_t x, uint8_t y) {
    if(x < width && y < height) {
        dateX = x;
        dateY = y;
    }
}

int DisplayLayout::calculateIndex(uint8_t x, uint8_t y) {
    if (x >= width || y >= height) return -1;
    // 修改前：return x * height + (height - 1 - y);
    // 修改后：直接计算索引，不做Y轴反转
    return x * height + y;
}

int DisplayLayout::getLedIndex(uint8_t x, uint8_t y) {
    return calculateIndex(x, y);
}

void DisplayLayout::drawDigit(CRGB* leds, uint8_t x, uint8_t y, uint8_t num, CRGB color) {
    if (num > 9 || !leds) return;
    
    for (uint8_t row = 0; row < 7; row++) {
        for (uint8_t col = 0; col < 3; col++) {
            if (digitPatterns[num][row][col]) {
                uint8_t posX = x + col;
                uint8_t posY = y + row;
                
                if (posX < width && posY < height) {
                    int index = calculateIndex(posX, posY);
                    if (index >= 0 && index < width * height) {
                        leds[index] = color;
                    }
                }
            }
        }
    }
}

void DisplayLayout::drawSmallDigit(CRGB* leds, uint8_t x, uint8_t y, uint8_t num, CRGB color) {
    if (num > 9 || !leds) return;
    
    for (uint8_t row = 0; row < 5; row++) {
        for (uint8_t col = 0; col < 3; col++) {
            if (smallDigitPatterns[num][row][col]) {
                uint8_t posX = x + col;
                uint8_t posY = y + row;
                
                if (posX < width && posY < height) {
                    int index = calculateIndex(posX, posY);
                    if (index >= 0 && index < width * height) {
                        leds[index] = color;
                    }
                }
            }
        }
    }
}

void DisplayLayout::drawColon(CRGB* leds, uint8_t x, uint8_t y, CRGB color) {
    if (!leds) return;
    
    // 上点
    if (x < width && y + 1 < height) {
        int index1 = calculateIndex(x, y + 1);
        if (index1 >= 0) {
            leds[index1] = color;
        }
    }
    
    // 下点
    if (x < width && y + 3 < height) {
        int index2 = calculateIndex(x, y + 3);
        if (index2 >= 0) {
            leds[index2] = color;
        }
    }
}

void DisplayLayout::drawCalendarIcon(CRGB* leds, uint8_t x, uint8_t y, CRGB color) {
    if (!leds) return;
    
    // 顶部横线
    for (uint8_t i = 0; i < 3; i++) {
        if (x + i < width && y < height) {
            leds[calculateIndex(x + i, y)] = color;
        }
    }
    
    // 顶部边框
    for (uint8_t i = 0; i < 3; i++) {
        if (x + i < width && y + 1 < height) {
            leds[calculateIndex(x + i, y + 1)] = color;
        }
    }
    
    // 左右边框
    if (x < width && y + 2 < height) {
        leds[calculateIndex(x, y + 2)] = color;
    }
    if (x + 2 < width && y + 2 < height) {
        leds[calculateIndex(x + 2, y + 2)] = color;
    }
    
    // 内部点
    if (x + 1 < width && y + 3 < height) {
        leds[calculateIndex(x + 1, y + 3)] = color;
    }
}

void DisplayLayout::setWeekPosition(uint8_t y) {
    if(y < height) {
        weekY = y;
    }
}

void DisplayLayout::drawWeekIndicator(CRGB* leds, uint8_t dayOfWeek, CRGB activeColor, CRGB inactiveColor) {
    if (!leds || dayOfWeek > 6 || weekY >= height) {
        return;
    }
    
    // 计算总宽度 (7条线 + 6个间隔)
    uint8_t totalWidth = 7 * weekLineWidth + 6 * weekSpacing;
    uint8_t startX = (width - totalWidth) / 2;
    
    // 确保起始位置在屏幕内
    if (startX + totalWidth > width) {
        startX = 0; // 如果超出屏幕，从最左边开始
    }
    
    // 绘制七条横线代表一周七天
    for (uint8_t i = 0; i < 7; i++) {
        CRGB lineColor = (i == dayOfWeek) ? activeColor : inactiveColor;
        
        // 绘制横线
        for (uint8_t j = 0; j < weekLineWidth; j++) {
            uint8_t x = startX + i * (weekLineWidth + weekSpacing) + j;
            if (x < width) {
                int index = calculateIndex(x, weekY);
                if (index >= 0 && index < width * height) {
                    leds[index] = lineColor;
                }
            }
        }
    }
}